import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paly_android/base/base_widget.dart';
import 'package:paly_android/bean/article_data_entity.dart';
import 'package:paly_android/bloc/project_tree_list_bloc.dart';
import 'package:paly_android/route/routes.dart';

class ProjectTreeList extends BaseWidget {
  @override
  BaseWidgetState<BaseWidget> getState() => _ProjectTreeListState();
}

class _ProjectTreeListState extends BaseWidgetState<ProjectTreeList> {
  late String title;
  late String cid;
  late ProjectTreeListBloc bloc;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    LogUtil.e("didChangeDependencies");
    showBackLeading=true;

    Map<String, dynamic> map =
        ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
    title = map["title"];
    cid = map["cid"].toString();

    // bloc = BlocProvider.of<ProjectTreeListBloc>(context);
    // bloc = context.read<ProjectTreeListBloc>();
    bloc = ProjectTreeListBloc();
    bloc.add(ProjectTreeListEvent(0, cid));
  }



  @override
  void preBuild(BuildContext context) {
    LogUtil.e("preBuild");
  }

  @override
  Widget baseBuild(BuildContext bcontext) {
    return BlocProvider(create: (context)=>bloc,
    child: BlocBuilder<ProjectTreeListBloc, ProjectTreeListState>(
        builder: (context, state) {

          return ListView.builder(

              itemCount: state.data.length,
              itemBuilder: (BuildContext context, int index) {
                var bean=state.data[index];

                return Column(
                  children: [
                    ListTile(title: Text(bean.title),
                      leading: Image.network(bean.envelopePic),
                      contentPadding: EdgeInsets.all(5),
                      onTap: (){
                        Navigator.of(bcontext).pushNamed(MyRouter.WEB,
                            arguments: {"title": bean.title, "url": bean.link});
                      },
                    ),
                    if (index<state.data.length-1) Divider()
                  ],
                );
              });
        }));
  }

  @override
  String getAppBarTitle() => title;
}
